package com.shiroa.controller;

import com.shiroa.common.R;
import com.shiroa.entity.SysRole;
import com.shiroa.service.ISysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-05-15
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {
    @Autowired
    private ISysRoleService roleService;

    @RequiresPermissions("role:add")
    @PostMapping
    public R add(@RequestBody SysRole role){
        try {
            roleService.save(role);
        }catch (Exception e){
            return R.err(e.getMessage());
        }
        return R.ok();
    }

    @RequiresRoles("manager")
    @DeleteMapping("/{roleId}")
    public R del(@PathVariable Long roleId){
        roleService.removeById(roleId);
        return R.ok();
    }

    @RequiresPermissions("user:update")
    @PutMapping
    public R update(@RequestBody SysRole role){
        roleService.saveOrUpdate(role);
        return R.ok();
    }

    @RequiresPermissions("user:list")
    @GetMapping
    public R getAll(){
        List<SysRole> list=roleService.list();
        return R.ok(list);
    }
}
